package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.OrderSettingDao;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.rmi.MarshalledObject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author LEO
 * @Date 2021年12月21日 1:38
 */
@Service(interfaceClass=OrderSettingService.class)  //发布服务，注册到zk中心
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void addBatch(List<OrderSetting> listData) {
        for (OrderSetting orderSetting : listData) {
            // 如果日期对应设置存在，就修改，否则添加
          int count=  orderSettingDao.findOrderSettingByOrderDate(orderSetting.getOrderDate());
          if (count>0){ // 预约设置存在
              orderSettingDao.edit(orderSetting);
          }else {
              orderSettingDao.addBatch(orderSetting);
          }
        }
    }

    /**  传递的参数
     *   date（格式：2019-8）
     *  构建的数据List<Map>
     *    map.put("date",1);
         map.put("number",120);
         map.put("reservations",10);

     *  查询方案：SELECT * FROM t_ordersetting WHERE orderDate LIKE '2019-08-%'
     *  查询方案：SELECT * FROM t_ordersetting WHERE orderDate BETWEEN '2019-9-1' AND '2019-9-31'
     */

//根据日期查询预约设置数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        // 1.组织查询Map，dateBegin表示月份开始时间，dateEnd月份结束时间
        String startDate = date + "-1";//2019-3-1
        String endDate = date + "-31";//2019-3-31
        Map param=new HashMap();
        param.put("startDate",startDate);
        param.put("endDate",endDate);
       List<OrderSetting> list=  orderSettingDao.getOrderSettingByMonth(param);

       List<Map> listData=new ArrayList<Map>();
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        sdf.format(orderSetting.getOrderDate())
        for (OrderSetting orderSetting : list) {
            /** 返回数据格式 Map
             * { date: 1, number: 120, reservations: 1 },
             *   { date: 3, number: 120, reservations: 1 },
             *   { date: 4, number: 120, reservations: 120 },
             *  { date: 6, number: 120, reservations: 1 },
             * { date: 8, number: 120, reservations: 1 }
             */
           Map map = new HashMap();
           map.put("date",orderSetting.getOrderDate().getDate());  // 返回 日
           map.put("number",orderSetting.getNumber());
           map.put("reservations",orderSetting.getReservations());
            listData.add(map);
        }
        return listData;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
            //根据日期修改可预约人数
            long count = orderSettingDao.findOrderSettingByOrderDate(orderSetting.getOrderDate());
            if(count > 0){
                //当前日期已经进行了预约设置，需要进行修改操作
                orderSettingDao.edit(orderSetting);
            }else{
                //当前日期没有进行预约设置，进行添加操作
                orderSettingDao.addBatch(orderSetting);
            }
    }


}
